import {Component, ViewChild} from '@angular/core';
import {AlertController, Content, IonicPage, NavController, NavParams, Refresher} from 'ionic-angular';
import {FoodMenuAddPage} from "./food-menu-add/food-menu-add";
import {AppUrl} from "../../../app/app.general";
import {HttpClient} from "@angular/common/http";
import {PageSplit} from "../../utils/page/page";
import {AppConfig} from "../../../app/appConfig";

/**
 * Generated class for the BusinessFoodMenuPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-business-food-menu',
  templateUrl: 'business-food-menu.html',
})
export class BusinessFoodMenuPage {
   //刷新不能固定菜品列表  放弃刷新
  // @ViewChild(Content) content: Content;
  // @ViewChild(Refresher) refresher: Refresher;
  // private page: PageSplit = {
  //   pageSize: 20,
  //   pageNo: 0,
  //   isLastPage: false
  // };

   typeId;

   getFoodListByType(){
     this.http.post(AppUrl.food.foodListByTId,{typeId: this.typeId}).subscribe(data => {

       let temp:any = data;
       if (temp.code === 200) {
         this.menuList =temp.data.list;
       }
       else {
         this.appConfig.presentToast(temp.msg);

       }
     });
   }

  // getData(refresher?: any) {
  //   this.page.pageNo++;
  //   this.http.post(AppUrl.food.foodList,{typeId: this.typeId,pageNo:this.page.pageNo,pageSize:this.page.pageSize}).subscribe(data => {
  //    if(refresher){
  //      this.refresher.complete();
  //      this.menuList = [];
  //    }
  //     let temp:any = data;
  //     if (temp.code === 200) {
  //       this.menuList = this.menuList.concat(temp.data.list);
  //       this.page.isLastPage = temp.data.isLastPage;
  //     }
  //     else {
  //       this.appConfig.presentToast(temp.msg);
  //
  //     }
  //   });
  // }

  // doInfinite(infiniteScroll): Promise<any> {
  //   return new Promise(resolve => {
  //     let that = this;
  //     setTimeout(function () {
  //       if (!that.page.isLastPage) {
  //         that.getData();
  //         resolve();
  //       }
  //       else {
  //         resolve();
  //       }
  //     }, 1000);
  //   });
  // }

  // doRefresh() {
  //   this.page = {
  //     isLastPage: false,
  //     pageSize: 20,
  //     pageNo: 0
  //   };
  //   this.getData(this.refresher);
  // }

  foodTypeList: Array<{id:number, foodtype:string,belong:string, chosen:boolean}>;
  constructor(public navCtrl: NavController, public navParams: NavParams,public nav:NavController,private http:HttpClient,public alerCtrl: AlertController, private appConfig: AppConfig) {
    this.foodTypeList =[];
  }

  getTypeList(){
    //获取菜品分类
    this.foodTypeList =[];
    this.http.post(AppUrl.foodType.selectByRestID, {restId: localStorage.getItem('restaurantid')}).subscribe(data => {
      let temp: any = data;
      // this.foodTypeList = temp.data;

      for(let list of temp.data) {
        this.foodTypeList.push({
          id: list.id,
          foodtype: list.foodtype,
          belong: list.belong,
          chosen: false
        });
      }

      this.foodTypeList[0].chosen = true;
      this.typeId = this.foodTypeList[0].id;


      // this.doRefresh();
      this.getFoodListByType();
    });
  }

  ionViewWillEnter(){

    // this.refresher._top = '50px';
    // this.refresher.state = 'refreshing';
    // this.refresher._beginRefresh();//模拟刷新
    this.getTypeList();
    // this.doRefresh();
    // this.getData();
  }

  doConfirm(id) {
    let confirm = this.alerCtrl.create({
      title: '删除菜品?',
      message: '请确认是否删除该菜品?',
      buttons: [
        {
          text: '不同意',
          handler: () => {
          }
        },
        {
          text: '同意',
          handler: () => {
            console.log("foodId:"+id);
            this.http.post(AppUrl.food.deleteFood,{foodId:id}).subscribe(data => {
              this.getFoodListByType();
            });
          }
        }
      ]
    });
    confirm.present()
  }

  menuList =[
    // {id:1,foodtype:'凉菜',foodname:'水腌菜'},
    // {id:2,foodtype:'小炒',foodname:'水腌菜'},
    // {id:3,foodtype:'汤',foodname:'水腌菜'},
    // {id:3,foodtype:'汤',foodname:'水腌菜'},
    // {id:3,foodtype:'汤',foodname:'水腌菜'},
    // {id:3,foodtype:'汤',foodname:'水腌菜'},
  ];

  ionViewDidLoad() {
    console.log('ionViewDidLoad BusinessFoodMenuPage');
  }

  addMenu(){
    this.nav.push('FoodMenuAddPage',{opType:'add'});
  }

  modifyFood(id,foodname,foodtype,price,stock,imgId,remark){
    this.nav.push('FoodMenuAddPage',{opType:'modify',id:id,foodname :foodname, foodtype:foodtype, price:price,
      stock:stock,imgId:imgId,remark:remark});
  }
  // getDataTmp = (data) => {
  //   return new Promise((resolve, reject) => {
  //
  //     this.getData();
  //     resolve();
  //   });
  // };
  lastNum:number =0;
  showFood(i,typeId) {
     this.typeId = typeId;
    this.foodTypeList[this.lastNum].chosen = false;
    this.lastNum = i;
    this.foodTypeList[i].chosen = true;

    // this.doRefresh();
    this.getFoodListByType();
  }
}
